<?php
	$files = $_POST['list'];
	$path = file_get_contents('../data/CurrentPath');
	$zipfile = '';
	
	if(count($files)== 1){
		if(is_dir($files[0]['fullname'])){
			$zipfile = $path.'/'.str_replace('.','_',$files[0]['basename']).'.zip';
		}else{
			$zipfile = $path.'/'.str_replace('.','_',$files[0]['filename']).'.zip';	
		}
		
	}else{
		$zipfile = $path.'/'.basename($path).'.zip';
	}
	function adddir($cpath,$path,$zip){
		$zip->addEmptyDir(str_replace($cpath.'/','',$path));
		$nodes = glob($path.'/*');		
		foreach($nodes as $node){
			if(is_dir($node)){
				adddir($cpath,$node,$zip);
			}else{				
				$zip->addFile($node,str_replace($cpath.'/','',$node));				
			}
		}
	}
	
	$zip = new ZipArchive;
	if($zip->open($zipfile, ZipArchive::CREATE) === TRUE){		
		
		foreach($files as $file){
			if($file['type'] == 'folder'){				
				adddir($file['dirname'],$file['dirname'].'/'.$file['basename'],$zip);	
			}else{
				$zip->addFile($file['dirname'].'/'.$file['basename'],$file['basename']);
			}				
		}
		$zip->close();
	}
	
?>